package com.jpcc.vw.onlineRadio.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cns.android.atmospherelamp.AtmosphereLampManager;
import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.adapter.TopRanksListAdapter;
import com.jpcc.vw.onlineRadio.base.BaseActivity;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.model.IRadio;
import com.jpcc.vw.onlineRadio.model.bean.BaseBean;
import com.jpcc.vw.onlineRadio.model.bean.TopListBean;
import com.jpcc.vw.onlineRadio.model.bean.TopListData;
import com.jpcc.vw.onlineRadio.model.impl.RadioImpl;
import com.jpcc.vw.onlineRadio.utils.CommonSubscriber;
import com.jpcc.vw.onlineRadio.utils.ToastUtils;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action0;

public class TopListRanksActivity extends BaseActivity {


    private IRadio radioModel;
    private boolean is_refresh = true;
    private int page_index = 1;
    private List<TopListData> datas = new ArrayList<>();
    private TopRanksListAdapter adapter;

    @BindView(R.id.swipe_refrsh_ranks)
    SmartRefreshLayout swipe_refrsh_ranks;
    @BindView(R.id.recyclerview_ranks)
    ListView recyclerview_ranks;
    @BindView(R.id.layout_back)
    RelativeLayout layout_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_show_empty)
    TextView tv_show_empty;
    @BindView(R.id.layout_search)
    RelativeLayout layout_search;
    @BindView(R.id.imv_top_bottom_line)
    ImageView imv_top_bottom_line;
    @BindView(R.id.progress_circular)
    ProgressBar progress_circular;

    @Override
    public int getLayoutId() {
        return R.layout.activity_toplist_ranks;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        radioModel = new RadioImpl();
        swipe_refrsh_ranks.setEnableRefresh(false);
        swipe_refrsh_ranks.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                is_refresh = true;
                swipe_refrsh_ranks.setEnableLoadmore(true);
                requestRadioRanks();
            }
        });
        layout_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TopListRanksActivity.this.finish();
            }
        });
        swipe_refrsh_ranks.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                is_refresh = false;
                requestRadioRanks();
            }
        });
        layout_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TopListRanksActivity.this, SearchActivity.class);
                startActivity(intent);
            }
        });
        adapter = new TopRanksListAdapter(this, datas);
        recyclerview_ranks.setAdapter(adapter);
        recyclerview_ranks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(TopListRanksActivity.this, TopListAlbumsActivity.class);
                intent.putExtra("key_rank_id", datas.get(position).getId());
                intent.putExtra("key_rank_name", datas.get(position).getTitle());
                startActivity(intent);
            }
        });
        is_refresh = true;
        requestRadioRanks();
        tv_title.setText(getResources().getString(R.string.string_18));
    }


    @Override
    public void doAtmosphere(boolean rgbOn, String color) {
        Logger.i("doAtmosphere" + "rgbOn-->" + rgbOn);
        Logger.i("doAtmosphere" + "color-->" + color);
        if (rgbOn) {
            if (TextUtils.isEmpty(color)) {
                imv_top_bottom_line.setColorFilter(Color.TRANSPARENT);
            } else {
                imv_top_bottom_line.setColorFilter(Color.parseColor(color));
            }
        } else {
            imv_top_bottom_line.setColorFilter(Color.TRANSPARENT);
        }
    }

    @Override
    public void setupData() {
        if (Constants.isRelease) {
            try {
                boolean atmosphereStatus = AtmosphereLampManager.getInstance(this).isHmiAtmosphereLampOn();
                String color = AtmosphereLampManager.getInstance(this).getHmiAtmosphereLampColor();
                doAtmosphere(atmosphereStatus, color);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    private void requestRadioRanks() {


//        if(Constants.isRelease) {
//            ConnectivityManager connectivityManager = (ConnectivityManager) TopListRanksActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
//            boolean connected = connectivityManager.getNetworkStatus();
//            if (!connected) {
//                ToastUtils.show(R.string.string_88);
//                return;
//            }
//        }


        int local_page = page_index;
        if (is_refresh) {
            local_page = 1;
        }

        radioModel.RadioRanks(local_page).doOnSubscribe(new Action0() {
            @Override
            public void call() {
//                showProgress();
                if(is_refresh){
                    progress_circular.setVisibility(View.VISIBLE);
                }
            }
        }).subscribe(new CommonSubscriber<TopListBean>() {
            @Override
            public void onSuccess(TopListBean response) {
//                hideProgress();
                progress_circular.setVisibility(View.INVISIBLE);
                swipe_refrsh_ranks.finishRefresh();
                swipe_refrsh_ranks.finishLoadmore();
                if (page_index > response.getData().getTotalPage() && !is_refresh) {
                    response.getData().setPageData(new ArrayList<>());
                }
                if (response.getData().getPageData() != null && response.getData().getPageData().size() > 0) {
                    tv_show_empty.setVisibility(View.GONE);
                    if (is_refresh) {
                        datas.clear();
                        page_index = 1;
                    }
                    page_index = page_index + 1;
                    datas.addAll(response.getData().getPageData());
                    adapter.setData(datas);
                    adapter.notifyDataSetChanged();
//                    page_index++;
                } else {
                    swipe_refrsh_ranks.setEnableLoadmore(false);
                    if (is_refresh) {
                        tv_show_empty.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onFailure(BaseBean response) {
//                hideProgress();
                progress_circular.setVisibility(View.INVISIBLE);
                ToastUtils.show(response.getStatusMessage());
                swipe_refrsh_ranks.finishRefresh();
                if (is_refresh) {
                    tv_show_empty.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}
